<template>
  <div class="container">
    <div class="userInfo" v-if="profile">
      <div class="userImg">
        <img :src="profile.avatarUrl" alt="" />
      </div>
      <p>{{ profile.nickname }}</p>
    </div>

    <!-- Grid -->
    <div class="grid">
      <van-grid>
        <van-grid-item icon="play-circle-o" text="最近播放" />
        <van-grid-item icon="desktop-o" text="本地下载" />
        <van-grid-item icon="upgrade" text="云盘" />
        <van-grid-item icon="ecard-pay" text="已购" />
        <van-grid-item icon="friends-o" text="我的好友" />
        <van-grid-item icon="discount" text="收藏" />
        <van-grid-item icon="bulb-o" text="我的博客" />
        <van-grid-item icon="volume-o" text="音乐罐子" />
      </van-grid>
    </div>

    <!-- likeMusic -->
    <div class="likeMusic" v-if="playList[0]" @click="goPlayListDetail">
      <div class="likeMusicImg">
        <img :src="playList[0].coverImgUrl" alt="" width="100%" />
      </div>
      <div class="title">
        <h5>我喜欢的音乐</h5>
        <span>{{ playList[0].trackCount }}首</span>
      </div>
    </div>

    <!-- playList -->
    <div class="myPlayListContainer">
      <div class="title">
        <span>我的歌单({{ playList.length }}个)</span>
        <div class="iconRight">
          <van-icon name="plus" class="plusIcon" />
          <van-icon name="bars" />
        </div>
      </div>
      <div
        class="playList"
        v-for="item in playList"
        :key="item.id"
        @click="playListDetail(item.id)"
      >
        <div class="imgContainer">
          <img :src="item.coverImgUrl" alt="" width="100%" />
        </div>
        <div class="playInfo">
          <h5>{{ item.name }}</h5>
          <span>{{ item.trackCount }}首</span>
        </div>
      </div>
    </div>

    <div class="logout">
      <van-button type="danger" size="large" round @click="logout">退出登录</van-button>
    </div>


    <div class="con"></div>

    
  </div>
</template>

<script>
import { mapState } from "vuex";
import { getUserPlayList, getPlayListDetail,logout} from "@/request/api";
export default {
  name: "UserVue",
  data() {
    return {
      playList: [],
      userLikeMusicID: [],
      musicId:[]
    };
  },
  computed: {
    ...mapState(["profile"]),
  },
  methods: {
    async getUserPlayList() {
      try {
        const { data: res } = await getUserPlayList({
          uid: this.profile.userId,
        });
        // console.log(res);
        if (res.code !== 200) return this.$toast("获取歌单失败");
        //请求成功
        this.playList = res.playlist;
      } catch (error) {
        console.log(error);
      }
    },
    //歌单详情
    async goPlayListDetail(id) {
      try {
        const { data: res } = await getPlayListDetail({
          id: this.playList[0].id,
        });
        console.log(res.playlist);
        if (res.code !== 200) return this.$toast("获取歌单详情失败");
        // this.$store.commit('GETMUSICID',res.playlist.trackIds)
        this.$router.push({
          name:'Playdetail'
        })
      } catch (error) {
        console.log(error.response.data.message);
      }
    },
    // 我的歌单详情
    async playListDetail(id) {
      // console.log(id);
      try {
        const { data: res } = await getPlayListDetail({id});
        console.log(res);
        if (res.code !== 200) return this.$toast("获取歌单详情失败");
        this.$store.commit('GETMUSICID',res.playlist.trackIds)
        this.$router.push({
          name:'Playdetail',
          query:{
            name:res.playlist.name
          }
        })
      } catch (error) {
        console.log(error.response.data.message);
      }
    },

    // 退出登录
    async logout(){
      const {data:res} = await logout()
      console.log(res);
      if(res.code === 200){
        this.$toast('退出成功')
          window.localStorage.removeItem('token')
          window.localStorage.removeItem('profile')
          this.$router.push('/login')
      }
    }
  },
  created() {
    this.getUserPlayList();
  },
};
</script>

<style lang="less" scoped>
.container {
  height: 100%;
  // background-color: rgb(248, 240, 230);
  padding: 0 3%;
}
.userInfo {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-top: 60px;
  background-color: #fff;
  height: 115px;
  box-shadow: 0px 0px 5px  rgba(0, 0, 0, 0.3);
  border-radius: 15px;
  text-align: center;
  .userImg {
    position: absolute;
    top: -50px;
    left: 135px;
    width: 100px;
    height: 100px;
    background-color: aqua;
    border-radius: 50%;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
    }
  }
  p {
    margin-top: 50px;
    font-size: 25px;
  }
}
.grid {
  margin-top: 20px;
  box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.3);
  height: 180px;
  border-radius: 15px;
}
.likeMusic {
  display: flex;
  align-items: center;
  margin-top: 20px;
  box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.3);
  height: 95px;
  border-radius: 15px;
  .likeMusicImg {
    width: 60px;
    height: 60px;
    margin: 0 12px;
    // border-radius: 20px;
    img {
      border-radius: 10px;
    }
  }
  .title {
    h5 {
      font-size: 18px;
    }
  }
}

.myPlayListContainer {
  margin: 15px 0;
  padding-bottom: 10px;
  box-shadow: 0px 0px 5px rgba(0, 0, 0, 0.3);
  border-radius: 15px;
  // height: 100px;
  .title {
    display: flex;
    padding: 0 8px;
    height: 40px;
    justify-content: space-between;
    align-items: center;
    color: rgb(107, 105, 105);
    font-size: 17px;
    .plusIcon {
      margin-right: 12px;
    }
  }
  .playList {
    display: flex;
    align-items: center;
    // background-color: red;
    height: 80px;
    .imgContainer {
      width: 60px;
      height: 60px;
      margin: 0 12px;
      img {
        border-radius: 10px;
      }
    }
    .playInfo {
      h5 {
        font-size: 18px;
      }
    }
  }
}
.con {
  height: 50px;
}
</style>